package com.ruoyi.system.service.front.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.util.StringUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.controller.CxBase;
import com.ruoyi.system.domain.StudentInfo;
import com.ruoyi.system.domain.front.*;
import com.ruoyi.system.mapper.StudentInfoMapper;
import com.ruoyi.system.mapper.front.StudentFrontMapper;
import com.ruoyi.system.service.connectUnity.IConnectService;
import com.ruoyi.system.service.front.IStudentFrontService;
import com.ruoyi.system.status.ConnectUrl;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StudentFrontServiceImpl extends CxBase implements IStudentFrontService {
    @Autowired
    private StudentInfoMapper studentInfoMapper;
    @Autowired
    private StudentFrontMapper studentFrontMapper;
    @Autowired
    private IConnectService connectService;
    /**
     * 修改联系方式
     *
     * @param updatePhone 联系方式
     * @return int
     */
    public int updatePhone(UpdatePhone updatePhone) {

        if (!checkNull(updatePhone.getStudentId())) {
            StudentInfo studentInfo = new StudentInfo();
            studentInfo.setStudentId(updatePhone.getStudentId());
            List<StudentInfo> studentInfos = new ArrayList<>();
            studentInfos = studentInfoMapper.selectStudentInfoList(studentInfo);
            //*设置更新
            studentInfos.get(0).setStudentPhone(updatePhone.getStudentPhone());
            studentInfos.get(0).setStudentMajor(updatePhone.getStudentMajor());
            return studentInfoMapper.updateStudentInfo(studentInfos.get(0));
        }
        return 0;
    }
    /**
     * 查询学生信息
     *
     * @param studentInfoFront 学号
     * @return 学生信息
     */
    public StudentInfoFront selectuserinfo(StudentInfoFront studentInfoFront) {
        if(!checkNull(studentInfoFront.getStudentId())) {
            return studentFrontMapper.selectuserinfo(studentInfoFront);
        }
        return null;
    }
    /**
     * 查询学生信息
     *
     * @param dicData 字典数据
     * @return 字典
     */
    public List<DicData> selectdic(DicData dicData) {
        if(!checkNull(dicData.getDicName())) {
            return studentFrontMapper.selectdic(dicData);
        }
        return null;
    }

    /**
     * 登录
     *
     * @param login 登录数据
     * @return 登录信息
     */
    public TableDataInfo loginfront(Login login){
        TableDataInfo tableDataInfo = new TableDataInfo();
        String loginPassword = connectService.PQpassword(login.getPassword());
        String secret = connectService.PQuserInfo(login.getStudentId());
        String url = ConnectUrl.ndkyeducheckPwd + "?appid="+ ConnectUrl.appid + "&secret=" + secret +"&username=" + login.getStudentId() + "&password=" + loginPassword;
        JSONObject passwordreturn = connectService.doGetJson(url);
        if(passwordreturn.getInteger("code") != null && passwordreturn.getInteger("code")==0){
            tableDataInfo = getDataTable(200,"登录成功");
        }
        else {
            tableDataInfo = getDataTable(400,"登录失败");
        }
        return tableDataInfo;
    }

    /**
     * 学生信息记录
     *
     * @param login 学生数据
     * @return 学生信息
     */
    public TableDataInfo writeUser(Login login){
        String secret = connectService.PQuserInfo(login.getStudentId());
        String url = ConnectUrl.ndkyeduuser + "?appid="+ ConnectUrl.appid + "&secret=" + secret +"&username=" + login.getStudentId();
        JSONObject userreturn = (JSONObject) JSONObject.parse(connectService.doGetJson(url).getString("message"));
        StudentInfo studentInfo1 = new StudentInfo();
        studentInfo1.setStudentId(login.getStudentId());
        if(studentInfoMapper.selectStudentInfoList(studentInfo1).size()<=0) {
            StudentInfo studentInfo = new StudentInfo();
            studentInfo.setStudentId(login.getStudentId());
            studentInfo.setStudentName(userreturn.getString("YHMC"));
            studentInfo.setStudentCollege(dictfind(userreturn.getString("BMMC")));
            studentInfo.setRole(userreturn.getString("YHLXMC"));
            studentInfoMapper.insertStudentInfo(studentInfo);
        }
        return getDataTable(200,"学生记录完成");
    }

    /**
     *
     *
     * 学生信息读取
     *
     * @param login 登录数据
     * @return 学生信息
     */
    public StudentInfo readUser(Login login){
        return studentInfoMapper.selectStudentInfoById(login.getStudentId());
    }

    /**
     * 查询学生中奖信息
     *
     * @param frontUserInfo 学生学号
     * @return 中奖信息
     */
    public List<PrizeWinRecordFrontStudent> selectPrizeWinRecordFrontStudent(FrontUserInfo frontUserInfo){
        if(!checkNull(frontUserInfo.getStudentId())){
            startPage();
            List<PrizeWinRecordFrontStudent> prizeWinRecordFrontStudents = studentFrontMapper.selectPrizeWinRecordFrontStudent(frontUserInfo);
            for(PrizeWinRecordFrontStudent prizeWinRecordFrontStudent:prizeWinRecordFrontStudents){
                prizeWinRecordFrontStudent.setPrizeWinDate(StampToString(Long.valueOf(prizeWinRecordFrontStudent.getPrizeWinDate())));
                if(prizeWinRecordFrontStudent.getReceiveType().equals('1'))
                {
                    prizeWinRecordFrontStudent.setPrizeWinPic(StampToString(Long.valueOf(prizeWinRecordFrontStudent.getPrizeWinPic())));
                }
            }
            return prizeWinRecordFrontStudents;
        }
        return null;
    }

    private String dictfind(String collegeName){
        DicData dicData = new DicData();
        dicData.setDicName("学院分类");
        List<DicData> dicDatas= studentFrontMapper.selectdic(dicData);
        int dicsoft = 0;
        int diclevelmax=0,diclevel;
        for(int i=0;i<dicDatas.size();i++){
            diclevel = StringUtils.getLevenshteinDistance(collegeName,dicDatas.get(i).getDicLabel());
            if(diclevelmax > diclevel || i==0) {
                dicsoft = dicDatas.get(i).getDicValue();
                diclevelmax = diclevel;
            }
        }
        return String.valueOf(dicsoft);
    }
}
